using System;
using Slobby.Common.NetworkFormatter;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Slobby.Server.Net {
    public class ClientConnection {

        public IPAddress Address {
            get {
                return ((IPEndPoint)connection.Client.RemoteEndPoint).Address;
            }
        }

        static NetworkFormatter formatter;
        static ClientConnection() {
            formatter = new NetworkFormatter(System.Reflection.Assembly.Load("SharedNetworkTypes"));
        }

        TcpClient connection;
        Stream stream;

        public ClientConnection(TcpClient connection) {
            this.connection = connection;
            stream = connection.GetStream();
        }

        public void SendMessage(object message) {
            formatter.Serialize(stream, message);
        }

        public object RecieveMessage() {
            return formatter.Deserialize(stream);
        }

        public void Disconnect() {
            connection.Close();
        }
    }
}
